package ldz.Show;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import ldz.User.*;
import xxh.BackgroundPanel;


public class RegistrationWindow extends JFrame {
    private JTextField unoField;
    private JTextField unameField;
    private JComboBox<String> usexComboBox;
    private JPasswordField upasswordField;
    private JTextField utelField;
    private JTextField uemailField;
    private JButton registerButton;
    private JButton backButton;

    public RegistrationWindow() {
        // 设置窗口标题
        setTitle("用户注册");
        // 设置窗口大小
        setSize(400, 350);
        // 设置窗口关闭操作
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        // 初始化组件并添加标题说明面板
        initComponentsWithTitle();

        // 显示窗口
        setVisible(true);
    }

    private void initComponentsWithTitle() {
        JPanel mainPanel = new JPanel(new BorderLayout());

        // 创建标题说明面板，使用FlowLayout布局并设置合适间距
        JPanel titlePanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 5));
        JLabel titleLabel = new JLabel("欢迎注册图书管理系统账号，请填写以下信息进行注册");
        titleLabel.setFont(new Font("宋体", Font.PLAIN, 14));
        titlePanel.add(titleLabel);
        mainPanel.add(titlePanel, BorderLayout.NORTH);

        // 创建用于放置返回按钮的面板，采用FlowLayout布局并左对齐
        JPanel northPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        backButton = new JButton("返回");
        backButton.addActionListener(new BackButtonListener());
        northPanel.add(backButton);
        mainPanel.add(northPanel, BorderLayout.WEST);

        // 用于放置用户信息输入相关组件的面板，使用GridBagLayout布局实现更灵活的定位

        BackgroundPanel inputPanel = new BackgroundPanel("src/xxh/img/bk.jpg",300,150);
        inputPanel.setLayout(new GridBagLayout());
//        JPanel inputPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);

        // 添加读者编号（Uno）相关组件
        gbc.gridx = 0;
        gbc.gridy = 0;
        inputPanel.add(new JLabel("账号："), gbc);
        gbc.gridx = 1;
        unoField = new JTextField(15);
        inputPanel.add(unoField, gbc);

        // 添加姓名（Uname）相关组件
        gbc.gridx = 0;
        gbc.gridy = 1;
        inputPanel.add(new JLabel("姓名："), gbc);
        gbc.gridx = 1;
        unameField = new JTextField(15);
        inputPanel.add(unameField, gbc);

        // 添加性别（Usex）相关组件
        gbc.gridx = 0;
        gbc.gridy = 2;
        inputPanel.add(new JLabel("性别："), gbc);
        String[] sexOptions = {"男", "女"};
        usexComboBox = new JComboBox<>(sexOptions);
        gbc.gridx = 1;
        inputPanel.add(usexComboBox, gbc);

        // 添加密码（Upassword）相关组件
        gbc.gridx = 0;
        gbc.gridy = 3;
        inputPanel.add(new JLabel("密码："), gbc);
        gbc.gridx = 1;
        upasswordField = new JPasswordField(15);
        inputPanel.add(upasswordField, gbc);

        // 添加电话（Utel）相关组件
        gbc.gridx = 0;
        gbc.gridy = 4;
        inputPanel.add(new JLabel("电话："), gbc);
        gbc.gridx = 1;
        utelField = new JTextField(15);
        inputPanel.add(utelField, gbc);

        // 添加邮箱（Uemail）相关组件
        gbc.gridx = 0;
        gbc.gridy = 5;
        inputPanel.add(new JLabel("邮箱："), gbc);
        gbc.gridx = 1;
        uemailField = new JTextField(15);
        inputPanel.add(uemailField, gbc);

        mainPanel.add(inputPanel, BorderLayout.CENTER);

        // 添加注册按钮
        registerButton = new JButton("注册");
        registerButton.addActionListener(new RegisterButtonListener());
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        buttonPanel.add(registerButton);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);

        add(mainPanel);
    }

    private class RegisterButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            String uno = unoField.getText();
            String uname = unameField.getText();
            String usex = (String) usexComboBox.getSelectedItem();
            char[] passwordChars = upasswordField.getPassword();
            String upassword = new String(passwordChars);
            String utel = utelField.getText();
            String uemail = uemailField.getText();

            // 使用UserDaoImpl来操作数据库插入用户信息
            UserDao userDao = new UserDaoImpl();
            int result = userDao.insertUser(uno, uname, usex, upassword, utel, uemail);
            if (result > 0) {
                JOptionPane.showMessageDialog(RegistrationWindow.this, "注册成功！");
                // 关闭当前注册窗口
                dispose();
                // 打开登录界面
                new LoginWindow().setVisible(true);
            } else {
                JOptionPane.showMessageDialog(RegistrationWindow.this, "注册失败，请检查输入信息是否合法或联系管理员。");
            }
        }
    }

    private class BackButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            // 关闭当前注册窗口
            dispose();
            // 打开登录界面
            LoginWindow loginWindow=new LoginWindow();
            loginWindow.setLocationRelativeTo(null);
            loginWindow.setVisible(true);

        }
    }
}